﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PointerClock
{
    public partial class MainForm : Form
    {
        #region 属性
        /// <summary>
        /// 实例化Clock
        /// </summary>
        Clock myclock = new Clock();
        /// <summary>
        /// 时钟刻线位置
        /// </summary>
        private Point pt1, pt2;
        /// <summary>
        /// 实例化Graphics
        /// </summary>
        System.Drawing.Graphics g;
        /// <summary>
        /// 画笔类
        /// </summary>
        Pen mypen;
        /// <summary>
        /// 闹钟状态
        /// </summary>
        private enum AlarmState
        {
            on,
            off
        }

        /// <summary>
        /// 当前状态
        /// </summary>
        private AlarmState _currentAlarmState;
        /// <summary>
        /// 当前状态
        /// </summary>

        private AlarmState CurrentAlarmState
        {
            get
            {
                return _currentAlarmState;
            }
            set
            {
                _currentAlarmState = value;
                if (_currentAlarmState == AlarmState.on)
                {
                    btnClock.Text = "已开";
                    btnClock.BackColor = Color.GreenYellow;
                }
                else if (_currentAlarmState == AlarmState.off)
                {
                    btnClock.Text = "已关";
                    btnClock.BackColor = Color.White;
                }
                else
                {
                   
                }

            }
        }
        
        /// <summary>
        /// 序列化实例
        /// </summary>
        private IFormatter formatter = new BinaryFormatter();

        /// <summary>
        /// 闹钟配置文件路径
        /// </summary>
        private string AlarmSetFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "AlarmClock.txt";
        #endregion

        #region 构造函数


        public MainForm()
        {
            InitializeComponent();
            timer1.Interval = 1000;//周期为1000ms            
            timer2.Interval = 1000;//周期为1000ms            
            timer1.Start();//启动时钟            
            timer2.Start();//启动时钟
            //读取闹钟设置文件
            try
            {
                if (File.Exists(AlarmSetFile))
                {
                    Stream readstream = new FileStream(AlarmSetFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                    Entity.AlarmClock alarm = (Entity.AlarmClock)formatter.Deserialize(readstream);
                    readstream.Close();
                    dateTimePicker1.Value = alarm.AlarmTime;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text);
            }
        }
        #endregion

        #region 事件
        private void timer1_Tick(object sender, EventArgs e)
        {

            mypen = new Pen(Color.Black, 3);//实例化一个画笔             
            for (int Hour = 0; Hour < 12; Hour++)
            {
                pt1 = new Point(Convert.ToInt32(myclock.Center_P.X + 100 * Math.Sin(Hour * Math.PI / 6.0)), Convert.ToInt32(myclock.Center_P.Y - 100 * Math.Cos(Hour * Math.PI / 6.0)));
                pt2 = new Point(Convert.ToInt32(myclock.Center_P.X + 110 * Math.Sin(Hour * Math.PI / 6.0)), Convert.ToInt32(myclock.Center_P.Y - 110 * Math.Cos(Hour * Math.PI / 6.0)));
                g = this.CreateGraphics();
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.DrawLine(mypen, pt1, pt2);
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Pen Hour_Pen = new Pen(Color.Black, 4);//小时的画笔    
            Pen Minute_Pen = new Pen(Color.Black, 3);//分针的画笔    
            Pen Second_Pen = new Pen(Color.Black, 1);//秒针的画笔   
            Graphics g = this.CreateGraphics();//绘图对象
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//抗锯齿模式，选highquality也是抗锯齿的 
            g.Clear(Color.White);//清除掉以前的时针，重新画 
            g.DrawLine(Second_Pen, myclock.Center_P, myclock.Second_P);//秒针 
            g.DrawLine(Minute_Pen, myclock.Center_P, myclock.Minute_P);//分针 
            g.DrawLine(Hour_Pen, myclock.Center_P, myclock.Hour_P);//时针 
            myclock.CalculateTime();//计算时间       
            if (DateTime.Now.ToString("hh:mm:ss") == dateTimePicker1.Value.ToString("hh:mm:ss"))
            {
                myclock.PlayAlarmClock();
            }
        }

        private void btnClock_Click(object sender, EventArgs e)
        {
            if (_currentAlarmState == AlarmState.on)
            {
                CurrentAlarmState = AlarmState.off;
            }
            else if (_currentAlarmState == AlarmState.off)
            {
                CurrentAlarmState = AlarmState.on;
            }
            else
            {

            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Entity.AlarmClock alart = new Entity.AlarmClock();
            alart.AlarmTime = dateTimePicker1.Value;
            Stream writestream = new FileStream(AlarmSetFile, FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(writestream, alart);
            writestream.Close();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            myclock.StopAlarmClock();
        }
        #endregion
    }
}
